//
//  FlutterNativeFacePlugin.m
//  Runner
//
//  Created by 小二同學 on 2021/1/15.
//  Copyright © 2021 The Chromium Authors. All rights reserved.
//

#import "FlutterNativeFacePlugin.h"
#import <WBCloudReflectionFaceVerify/WBFaceVerifyCustomerService.h>

#define LICENCE @"W4IZq5Ech1Lt8yf9RavQQy02iB0jMYVdFvsGjkWMkzueGTvnDHMPfaB6gfQU8Ynl+WrHkhHzwvceig7djeG2SjO2ogk7U1SYlnrEU2MFW0r4vk11lHVsxrJcDYDOofOpDFoQnYV4/qBfItNSuTN5veb2vH3Xa8TvhXMWqRSg/HRQcadZyHTt3vEyYdz66NPOL97GQkUqAFHCKcmOPu886L7LmQmF9au/GmaWIRKEFGvmefstORQ3lV1r1ijxWeNirgO16p71rL3LNc2fy5rBEbcvRlO0rJLAcovkj84PFkNAwRliYFKyu+4Z+bQ2eKbltFkS5WHvWD9QTgm70Qbmmg=="

@interface FlutterNativeFacePlugin()<WBFaceVerifyCustomerServiceDelegate>

@property (nonatomic, strong) UIViewController *controller;

@property(nonatomic, copy) FlutterResult result;

@end

@implementation FlutterNativeFacePlugin

static NSString *const CHANNELNAME = @"com.ahj/txFaceshibie";
static NSString *const METHODNAME  = @"FaceShiBie";

/**
 *plugin 注册
 */
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:CHANNELNAME binaryMessenger:[registrar messenger]];
    
    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    FlutterNativeFacePlugin *plugin = [[FlutterNativeFacePlugin alloc]initWithViewController:viewController];
    
    [registrar addMethodCallDelegate:plugin channel:channel];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wbSDKServiceDidFinishedNotification:) name:WBFaceVerifyCustomerServiceDidFinishedNotification object:nil];
}

/**
 *flutter端通过通道调用原生方法时会进入以下回调
 */
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    
    NSString *methodName = call.method;
    if ([methodName isEqualToString:METHODNAME]) {
        //flutter传给原生的参数
        NSDictionary *para = call.arguments;
        NSLog(@"flutter传给原生的参数：%@", para);
        [self faceVerifyEntranceUsingFaceIdWithParams:para];
        self.result = result;
    }else if ([methodName isEqualToString:@"openSetting"]) {
//        [self openSetting];
    } else {
        result(FlutterMethodNotImplemented);
    }
}

- (void)faceVerifyEntranceUsingFaceIdWithParams:(NSDictionary *)dic {
    [WBFaceVerifyCustomerService sharedInstance].delegate = self;
    WBFaceVerifySDKConfig *config = [self getSDKSettings];
    
    [[WBFaceVerifyCustomerService sharedInstance] initSDKWithUserId:dic[@"userid"] nonce:dic[@"nonce"] sign:dic[@"sign"] appid:@"IDAobdd0" orderNo:dic[@"orderNo"] apiVersion:@"1.0.0" licence:LICENCE faceType:WBFaceVerifyLivingType_Action faceId:dic[@"faceId"] sdkConfig:config success:^{
        [[WBFaceVerifyCustomerService sharedInstance] startWbFaceVeirifySdk];

    } failure:^(WBFaceError * _Nonnull error) {
        
    }];
}

#pragma mark - WBFaceVerifyCustomerServiceDelegate
-(void)wbfaceVerifyCustomerServiceDidFinishedWithFaceVerifyResult:(WBFaceVerifyResult *)faceVerifyResult{
    NSLog(@"result 结果: %@", faceVerifyResult);
    self.result(faceVerifyResult.isSuccess ? @"1" : @"0");
}

- (WBFaceVerifySDKConfig *)getSDKSettings {
    WBFaceVerifySDKConfig *config = [WBFaceVerifySDKConfig sdkConfig];
    config.manualCookie = YES;//扫描面部信息，与主头像对比
    config.useWindowSecene = NO;
    return  config;
}

- (instancetype)initWithViewController:(UIViewController *)viewController {
    self = [super init];
    if (self) {
        self.controller = viewController;
    }
    return self;
}

//- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}
@end
